package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/*
demo表修改一条记录
 */
public class JDBCUtil_3 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;

        try {
            //注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //定义sql语句
            String sql = "update demo set name = '小明'where id = 4";
            //链接connection对象
             conn = DriverManager.getConnection("jdbc:mysql:///sanqiansql?serverTimezone=GMT", "root", "shaozhaoming");
            //获取执行sql的对象
             stmt = conn.createStatement();
             //执行sql
            int count = stmt.executeUpdate(sql);
            System.out.println(count);
            if(count >0){
                System.out.println("修改成功");
            }else{
                System.out.println("修改失败");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            if(stmt!=null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
